home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter9 / hotkey.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  238 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "hotkey.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "Hot-Key"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HWND hHotKey = NULL;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_CREATE :
  114.               InitCommonControls();
  115.  
  116.               // Create the edit control.
  117.               //.........................
  118.               hHotKey = CreateWindowEx( WS_EX_CLIENTEDGE, HOTKEY_CLASS, "",
  119.                                         WS_CHILD | WS_BORDER | WS_VISIBLE,
  120.                                         40, 40, 100, 25, hWnd, 
  121.                                         (HMENU)1, hInst, NULL );
  122.  
  123.               // Make ALT the default modifier.
  124.               //...............................
  125.               SendMessage( hHotKey, HKM_SETRULES, 
  126.                            (WPARAM) HKCOMB_NONE | HKCOMB_S,
  127.                             MAKELPARAM( HOTKEYF_ALT, 0) );
  128.  
  129.               // Set CTRL + ALT + A as the default
  130.               // hot key for this window. 
  131.               //..................................
  132.               SendMessage( hHotKey, HKM_SETHOTKEY, 
  133.                            MAKEWORD( 0x41, 
  134.                            HOTKEYF_CONTROL | HOTKEYF_ALT), 0); 
  135.  
  136.               // Associate the hot key with the window.
  137.               //.......................................
  138.               SendMessage( hWnd, WM_SETHOTKEY, 
  139.                            MAKEWORD( 0x41, 
  140.                            HOTKEYF_CONTROL | HOTKEYF_ALT), 0 );
  141.               break;
  142.  
  143.  
  144.       case WM_COMMAND :
  145.               switch( LOWORD( wParam ) )
  146.               {
  147.                  case IDM_SETHOTKEY :
  148.                         {
  149.                            WORD wHotkey;
  150.                            UINT iSetResult; 
  151.  
  152.                            // Retrieve the hot key. 
  153.                            //......................
  154.                            wHotkey = (WORD)SendMessage( hHotKey,
  155.                                             HKM_GETHOTKEY, 0, 0 ); 
  156.  
  157.                            // Associate the hot
  158.                            // key with the window.
  159.                            //.....................
  160.                            iSetResult = SendMessage( hWnd, 
  161.                                             WM_SETHOTKEY, wHotkey, 0 ); 
  162.  
  163.                            switch ( iSetResult ) 
  164.                            { 
  165.                               case 2 :
  166.                                    MessageBox( hWnd, 
  167.                                             "Hot key previously assigned", 
  168.                                             lpszTitle, MB_OK ); 
  169.                                 break;
  170.  
  171.                               case 1 : // Successful
  172.                                 break;
  173.  
  174.                               case 0 : 
  175.                                    MessageBox( hWnd, 
  176.                                             "Invalid window for hot key", 
  177.                                             "Error", MB_OK ); 
  178.                                 break;
  179.              
  180.                               case -1 : 
  181.                                    MessageBox( hWnd, "Invalid hot key", 
  182.                                             "Error", MB_OK); 
  183.                                 break;
  184.  
  185.                               default: 
  186.                                    MessageBox( hWnd, 
  187.                                             "Unknown error", 
  188.                                             "Error", MB_OK); 
  189.                                 break;
  190.                            }
  191.                         }
  192.                         break;
  193.  
  194.                  case IDM_ABOUT :
  195.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  196.                         break;
  197.  
  198.                  case IDM_EXIT :
  199.                         DestroyWindow( hWnd );
  200.                         break;
  201.               }
  202.               break;
  203.       
  204.       case WM_DESTROY :
  205.               PostQuitMessage(0);
  206.               break;
  207.  
  208.       default :
  209.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  210.    }
  211.  
  212.    return( 0L );
  213. }
  214.  
  215.  
  216. LRESULT CALLBACK About( HWND hDlg,           
  217.                         UINT message,        
  218.                         WPARAM wParam,       
  219.                         LPARAM lParam)
  220. {
  221.    switch (message) 
  222.    {
  223.        case WM_INITDIALOG: 
  224.                return (TRUE);
  225.  
  226.        case WM_COMMAND:                              
  227.                if (   LOWORD(wParam) == IDOK         
  228.                    || LOWORD(wParam) == IDCANCEL)    
  229.                {
  230.                        EndDialog(hDlg, TRUE);        
  231.                        return (TRUE);
  232.                }
  233.                break;
  234.    }
  235.  
  236.    return (FALSE); 
  237. }
  238.